package cn.bluarry.litepaltest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static String TAG="Log_Main";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: ");
        Button btn=findViewById(R.id.btn);
        Button add_btn=findViewById(R.id.add_btn);
        Button update_btn=findViewById(R.id.update_btn);
        Button delete_btn=findViewById(R.id.delete_btn);
        Button query_btn=findViewById(R.id.query_btn);
        btn.setOnClickListener(this);
        add_btn.setOnClickListener(this);
        update_btn.setOnClickListener(this);
        delete_btn.setOnClickListener(this);
        query_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Book book;
        switch (v.getId()){
            case R.id.btn:
                Connector.getDatabase();
                Log.d(TAG, "数据库加载!");
                break;
            case R.id.add_btn:
                book=new Book();
                book.setAuthor("aaaa");
                book.setName("政府");
                book.setPages(123);
                book.setPress("aasad");
                book.setPrice(12.12);
                book.save();
                Log.d(TAG, "添加完成");
                break;
            case R.id.update_btn:
//                book=new Book();
//                book.setAuthor("bbbb");
//                book.setName("政府");
//                book.setPages(321);
//                book.setPress("aasad");
//                book.setPrice(45.65);
//                book.save();
//                book.setPages(789);
//                book.save();
                book=new Book();
                book.setName("hello");
                book.updateAll("id=?","2");
                Log.d(TAG, "更新完成");
                break;
            case R.id.delete_btn:
                LitePal.deleteAll(Book.class,"id=?","2");
                Log.d(TAG, "删除完成");
                break;
            case R.id.query_btn:
                List<Book> books=LitePal.findAll(Book.class);
                for(Book b:books){
                    Log.d(TAG, "name= "+b.getName()
                                    +" author ="+b.getAuthor()
                                    +" pages ="+b.getPages()
                    );
                }

                Log.d(TAG, "查询完成");
                break;
            default:
                Log.d(TAG, "暂未设置点击事件");
                break;
        }
    }
}
